<?php

require_once '../include/common.inc.php';

if (!checkAdmin()) {
   header("LOCATION:index.php");
   exit;
}
$menu_index = 2;
$page_title = 'TABLA';
$guide_message = 'Customer | Promoter Order List';


getMyParam('pid');//promoter id

$page_title = 'TABLA';
$page_url = "c_promoter_order.php?pid=".$pid."&";
$ppp =20;

getMyParam('Submit');
if($Submit!="")
{
	$ckbs=$_POST["ckb"];
	getMyParam('pay_date');
	for($i=0;$i<count($ckbs);$i++)
	{
		$sql="update promoter_commission set status=1,pay_date=$pay_date where order_id=".$ckbs[$i];
		$mysql->query($sql);
	}
}
if($action!="")
{
    getMyParam('status');
	getMyParam('pay_date');				
	getMyParam('cid');	
	if ($action =="edit") {
			$pay_date=trim($pay_date);
			$sql="update `promoter_commission` SET `pay_date` = '".strtotime($pay_date)."',
				`status` = '".$status."' where id=".$cid;
			$db->query($sql);
			$msg="Successfully updated promoter_commission info.";
			$href="c_promoter_order.php?pid=$pid";
			include_once('menu_list.php');
			require_once PrintEot('a_header');
			require_once PrintEot('a_msg2');
			require_once PrintEot('a_footer');
			exit;
	
	} 
	if ($action == "del") {
		if (is_numeric($cid)) {
			$sql = "DELETE FROM `promoter_commission` WHERE `id` = '" . $cid . "'";
			$db->query($sql);
			$msg="This promoter commission has been deleted. ";
			$href="c_promoter_order.php?pid=$pid";
			include_once('menu_list.php');
			require_once PrintEot('a_header');
			require_once PrintEot('a_msg2');
			require_once PrintEot('a_footer');
			exit;
		}
	}
}
$disable2="";//如果promoter没有选择换购，多选框不可用
$edit_style="";//如果promoter没有选择换购，无编辑按钮
//promoter info
$sql = "SELECT * FROM  promoter_info  where id=".$pid;
$ary_promoter = $mysql->fetch($sql);
$pname=$ary_promoter[0]["firstname"].$ary_promoter[0]["lastname"];
if($ary_promoter[0]["faq1"]=="1")
{
	$disable2="disabled=\"disabled\"";
	$edit_style=" style=\"display:none;\"";
}
$ary_rank=getRankAry($ary_promoter[0]["rank"]);
$str_pay_date="";
$pay_year=date(Y);
for($i=3;$i<count($ary_rank);$i++)
{
	$ary_date_tmp=explode("/",$ary_rank[$i]);
	$utime=strtotime($pay_year."-".$ary_date_tmp[1]."-".$ary_date_tmp[0]);
	$str_pay_date.="<option value=\"$utime\">$ary_rank[$i]</option>";
}




$sql="SELECT co.*,concat(cusm.firstname,cusm.lastname) as customer,pc.id as pcid,pc.cost as pccost,pc.commission,pc.status,pc.pay_date FROM  cus_order co,promoter_commission pc,cus_members cusm where pc.promoter_id=$pid and co.user_id=cusm.id and co.id=pc.order_id and co.pay_flag=0";
$query = $db->query($sql);
$sql_all_num = $db->num_rows($query);
// *** page code start ***
getMyParam('page');
$page = empty($page) || !ispage($page) ? 1 : $page;
$start_limit = ($page - 1) * $ppp;
if($start_limit > $sql_all_num) {
    $start_limit = 0;
    $page = 1;
}

// *** page code end ***



$sql = "SELECT co.*,concat(cusm.firstname,cusm.lastname) as customer,pc.id as pcid,pc.cost as pccost,pc.commission,pc.status,pc.pay_date FROM  cus_order co,promoter_commission pc,cus_members cusm where pc.promoter_id=$pid and co.user_id=cusm.id and co.id=pc.order_id and co.pay_flag=0 ORDER BY co.id DESC LIMIT $start_limit, $ppp";
$query = $db->query($sql);
while($rs = $db->fetch_array($query)) {
    $out_ary[] = $rs;
}


for($i=0;$i<count($out_ary);$i++)
{
	//ʾ
	$out_ary[$i]['date']=date('Y-m-d',$out_ary[$i]['date']);
	if($out_ary[$i]['pay_date']!="" &&$out_ary[$i]['status']==1)
		$out_ary[$i]['pay_date']=date('Y-m-d',$out_ary[$i]['pay_date']);
	else
		$out_ary[$i]['pay_date']="";
	//״̬
	if($out_ary[$i]['status']>0) $out_ary[$i]['payment']="Yes";
	else $out_ary[$i]['payment']="No";
	//Ͷ״̬
	if($out_ary[$i]['deliver_status']==0) $out_ary[$i]['deliver_status']="Non-deliver";
	else if($out_ary[$i]['deliver_status']==1) $out_ary[$i]['deliver_status']="Delivered";	
	else $out_ary[$i]['deliver_status']="Cancelled";
}
$asa=0;
$ac=0;
$co=0;
$cms=getCms($pid);
$asa=$cms["cost"];
$ac=$cms["cms"];
$co=$cms["cms_osd"];

$page_show = getPageStr($sql_all_num, $page, $ppp, $page_url);
///////////////////////////////佣金换购产品的订单列表///////////////////////////////////////////////////////////////
//
$sql = "SELECT `id` FROM `promoter_order` where promoter_id=".$pid;
$query = $db->query($sql);
$sql_all_num_p = $db->num_rows($query);

// *** page code start ***
getMyParam('page_p');
$page_p = empty($page_p) || !ispage($page_p) ? 1 : $page_p;
$start_limit_p = ($page_p - 1) * $ppp;
if($start_limit_p > $sql_all_num_p) {
    $start_limit_p = 0;
    $page_p = 1;
}
// *** page code end ***

$sql = "SELECT * FROM  promoter_order where promoter_id=$pid ORDER BY `id` DESC LIMIT $start_limit_p, $ppp";
$query = $db->query($sql);
while($rs = $db->fetch_array($query)) {
    $out_ary_p[] = $rs;
}


for($i=0;$i<count($out_ary_p);$i++)
{
	$out_ary_p[$i]['date']=date('Y-m-d',$out_ary_p[$i]['date']);
	$out_ary_p[$i]['paidbycms']=$out_ary_p[$i]['cost']-$out_ary_p[$i]['pay_amount'];
	$out_ary_p[$i]['paidbycms']=number_format($out_ary_p[$i]['paidbycms'],2);
	$out_ary_p[$i]['pay_amount']=number_format($out_ary_p[$i]['pay_amount'],2);

	if($out_ary_p[$i]['deliver_status']!=1)
		$out_ary_p[$i]['edit_link']="<a href=\"c_promoter_commission_edit.php?cid=".$out_ary[$i]['pcid']." ".$edit_style."><img src=\"../images/admin/bt_edit.gif\" border=\"0\"></a>";
	else
		$out_ary_p[$i]['edit_link']="";	
		
	if($out_ary_p[$i]['pay_flag']==0)
	{
	if($out_ary_p[$i]['deliver_status']==0)$out_ary_p[$i]['deliver_status']="Non-deliver";
	elseif($out_ary_p[$i]['deliver_status']==1) $out_ary_p[$i]['deliver_status']="Delivered";
	else $out_ary_p[$i]['deliver_status']="Cancelled";
	}
	else
	{
		$out_ary_p[$i]['deliver_status']="Aborted";
	}

}

$page_show_p = getPageStr($sql_all_num_p, $page_p, $ppp, $page_url);

//////////////////////////////////////////////////////////////////////////////////////////////

include_once('menu_list.php');
require_once PrintEot('a_header');
require_once PrintEot('a_c_promoter_order');
require_once PrintEot('a_footer');

?>
